/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/02/17
* File: vector2.h
* Blog: http://www.cnblogs.com/zengqh/
**/

#pragma once

#include "math_def.h"

namespace HY
{
/** Two-dimensional vector. */
class Vector2
{
public:
	/// Construct undefined.
	Vector2()
	{
	}

	/// Copy-construct from another vector.
	Vector2(const Vector2& vector) :
	x_(vector.x_),
		y_(vector.y_)
	{
	}

	/// Construct from coordinates.
	Vector2(float x, float y) :
	x_(x),
		y_(y)
	{
	}

	/// Construct from a float array.
	Vector2(const float* data) :
	x_(data[0]),
		y_(data[1])
	{
	}

	/// Assign from another vector.
	Vector2& operator = (const Vector2& rhs)
	{
		x_ = rhs.x_;
		y_ = rhs.y_;
		return *this;
	}

	/// Test for equality with another vector without epsilon.
	bool operator == (const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
	/// Test for inequality with another vector without epsilon.
	bool operator != (const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
	/// Add a vector.
	Vector2 operator + (const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
	/// Return negation.
	Vector2 operator - () const { return Vector2(-x_, -y_); }
	/// Subtract a vector.
	Vector2 operator - (const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
	/// Multiply with a scalar.
	Vector2 operator * (float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
	/// Multiply with a vector.
	Vector2 operator * (const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
	/// Divide by a scalar.
	Vector2 operator / (float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
	/// Divide by a vector.
	Vector2 operator / (const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }

	/// Add-assign a vector.
	Vector2& operator += (const Vector2& rhs)
	{
		x_ += rhs.x_;
		y_ += rhs.y_;
		return *this;
	}

	/// Subtract-assign a vector.
	Vector2& operator -= (const Vector2& rhs)
	{
		x_ -= rhs.x_;
		y_ -= rhs.y_;
		return *this;
	}

	/// Multiply-assign a scalar.
	Vector2& operator *= (float rhs)
	{
		x_ *= rhs;
		y_ *= rhs;
		return *this;
	}

	/// Multiply-assign a vector.
	Vector2& operator *= (const Vector2& rhs)
	{
		x_ *= rhs.x_;
		y_ *= rhs.y_;
		return *this;
	}

	/// Divide-assign a scalar.
	Vector2& operator /= (float rhs)
	{
		float invRhs = 1.0f / rhs;
		x_ *= invRhs;
		y_ *= invRhs;
		return *this;
	}

	/// Divide-assign a vector.
	Vector2& operator /= (const Vector2& rhs)
	{
		x_ /= rhs.x_;
		y_ /= rhs.y_;
		return *this;
	}

	/// Normalize to unit length and return the previous length.
	float Normalize()
	{
		float len = Length();
		if (len >= HY_EPSILON)
		{
			float invLen = 1.0f / len;
			x_ *= invLen;
			y_ *= invLen;
		}

		return len;
	}

	/// Return length.
	float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
	/// Return squared length.
	float LengthSquared() const { return x_ * x_ + y_ * y_; }
	/// Calculate dot product.
	float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
	/// Calculate absolute dot product.
	float AbsDotProduct(const Vector2& rhs) const { return HY::Abs(x_ * rhs.x_) + HY::Abs(y_ * rhs.y_); }
	/// Return absolute vector.
	Vector2 Abs() const { return Vector2(HY::Abs(x_), HY::Abs(y_)); }
	/// Linear interpolation with another vector.
	Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
	/// Test for equality with another vector with epsilon.
	bool Equals(const Vector2& rhs) const { return HY::Equals(x_, rhs.x_) && HY::Equals(y_, rhs.y_); }

	/// Return normalized to unit length.
	Vector2 Normalized() const
	{
		float len = Length();
		if (len >= HY_EPSILON)
			return *this * (1.0f / len);
		else
			return *this;
	}

	/// Return float data.
	const float* Data() const { return &x_; }

	/// X coordinate.
	float x_;
	/// Y coordinate.
	float y_;

	/// Zero vector.
	static const Vector2 ZERO;
	/// (-1,0) vector.
	static const Vector2 LEFT;
	/// (1,0) vector.
	static const Vector2 RIGHT;
	/// (0,1) vector.
	static const Vector2 UP;
	/// (0,-1) vector.
	static const Vector2 DOWN;
	/// (1,1) vector.
	static const Vector2 ONE;
};

/// Multiply Vector2 with a scalar
inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }

/// Two-dimensional vector with integer values.
class IntVector2
{
public:
	/// Construct undefined.
	IntVector2()
	{
	}

	/// Construct from coordinates.
	IntVector2(int x, int y) :
	x_(x),
		y_(y)
	{
	}

	/// Copy-construct from another vector.
	IntVector2(const IntVector2& rhs) :
	x_(rhs.x_),
		y_(rhs.y_)
	{
	}

	/// Test for equality with another vector.
	bool operator == (const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
	/// Test for inequality with another vector.
	bool operator != (const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
	/// Add a vector.
	IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
	/// Return negation.
	IntVector2 operator - () const { return IntVector2(-x_, -y_); }
	/// Subtract a vector.
	IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
	/// Multiply with a scalar.
	IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
	/// Divide by a scalar.
	IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }

	/// Add-assign a vector.
	IntVector2& operator += (const IntVector2& rhs)
	{
		x_ += rhs.x_;
		y_ += rhs.y_;
		return *this;
	}

	/// Subtract-assign a vector.
	IntVector2& operator -= (const IntVector2& rhs)
	{
		x_ -= rhs.x_;
		y_ -= rhs.y_;
		return *this;
	}

	/// Multiply-assign a scalar.
	IntVector2& operator *= (int rhs)
	{
		x_ *= rhs;
		y_ *= rhs;
		return *this;
	}

	/// Divide-assign a scalar.
	IntVector2& operator /= (int rhs)
	{
		x_ /= rhs;
		y_ /= rhs;
		return *this;
	}

	/// Return integer data.
	const int* Data() const { return &x_; }

	/// X coordinate.
	int x_;
	/// Y coordinate.
	int y_;

	/// Zero vector.
	static const IntVector2 ZERO;
};

/// Multiply IntVector2 with a scalar.
inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }

}